// JavaScript Document
$(function() {
	
	var nav = $("ul#navigation li").find("a"),
		index = nav.eq(0),
		commer = nav.eq(1),
		domestic = nav.eq(2),
		lawn = nav.eq(3),
		wind = nav.eq(4),
		subcontract = nav.eq(5);
	if($("h1#index").size() > 0){
		index.addClass("active");
	}else if($("h1#commercial").size() > 0){
		commer.addClass("active");
	}else if($("h1#window").size() > 0){
		wind.addClass("active");
	}else if($("h1#sub").size() > 0){
		subcontract.addClass("active");
	}else if($("h1#lawn").size() > 0){
		lawn.addClass("active");
	}else if($("h1#home").size() > 0){
		domestic.addClass("active");
	}
	/*	var url = window.location.pathname, 
        urlRegExp = new RegExp(url.replace(/\/$/,'') + "$"); // create regexp to match current url pathname and remove trailing slash if present as it could collide with the link in navigation in case trailing slash wasn't present there
        // now grab every link from the navigation
      nav.each(function() {
            // and test its normalized href against the url pathname regexp
            if(urlRegExp.test(this.href.replace(/\/$/,''))){
                $(this).addClass('active');
            }
        });*/

	$("span.btn_submit").click(function(e){
		
		var input = $("div.sidebar-wrap").find(":input"),
			inquiry = input.eq(0),
			service = input.eq(1),
			fname = input.eq(2),
			email = input.eq(3),
			phoneNo = input.eq(4),
			address = input.eq(5),
			err = $(":input.error_field").size(),
			count = 0;
			
			/*input.click( function(){
				inquiry.removeClass("error_field");
			});*/
			
				function showError (){
					var e = $("span.error_msg").text("Please complete all required fields.").show()
						.end().find("span.success_msg").hide();
					return e;
				}
				if(!inquiry[0].selectedIndex>0){
					inquiry.addClass("error_field");
					showError();
					count++;
				}else{
					inquiry.removeClass("error_field");
				}
				if(!service[0].selectedIndex>0){
					service.addClass("error_field");
					showError();
					count++;
				}else{
					service.removeClass("error_field");
				}
				if(fname.val() == ""){
					fname.addClass("error_field");
					showError();
					count++;
				}else{
					fname.removeClass("error_field");
				}
				if(email.val() == ""){
					email.addClass("error_field");
					showError();
					count++;
				}else{
					email.removeClass("error_field");
				}
				if(phoneNo.val() == ""){
					phoneNo.addClass("error_field");
					showError();
					count++;
				}else{
					phoneNo.removeClass("error_field");
				}
				if(address.val() == ""){
					address.addClass("error_field");
					showError();
					count++;
				}else{
					address.removeClass("error_field");
				}
					
			if(count != 0){
				e.preventDefault();				
			}else {
				/*$("div.error").find("span").text("Thanks for your message, we'll get back to you as soon as possible.").addClass("success_msg");*/ 
					if($("span.textfieldRequiredState").size() == 0 && $("span.textfieldInvalidFormatState").size() == 0 && $("span.textfieldMinCharsState").size() == 0){
						$("form#contact").submit();
					}
				}
		});
	
});